﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Maticsoft.Common;
using LTP.Accounts.Bus;
namespace XDSoft.Web.TB_Brand
{
    public partial class Add : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            XDSoft.BLL.TB_Options bll = new XDSoft.BLL.TB_Options();
            this.ddlStatus.DataSource = bll.GetModelList("parent='" + EnumCommon.OptionsType.BrandStatus.ToString() + "'");
            this.ddlStatus.DataTextField = "name";
            this.ddlStatus.DataValueField = "id";
            this.ddlStatus.DataBind();

            this.ddlBrandType.DataSource = bll.GetModelList("parent='" + EnumCommon.OptionsType.BrandType.ToString() + "'");
            this.ddlBrandType.DataTextField = "name";
            this.ddlBrandType.DataValueField = "id";
            this.ddlBrandType.DataBind();

            this.ddlTypeCode.DataSource = bll.GetModelList("parent='" + EnumCommon.OptionsType.InternationalCode.ToString() + "'");
            this.ddlTypeCode.DataTextField = "name";
            this.ddlTypeCode.DataValueField = "id";
            this.ddlTypeCode.DataBind();

            XDSoft.BLL.TB_Clients cBll = new XDSoft.BLL.TB_Clients();
            this.ddlClient.AppendDataBoundItems = true;
            this.ddlClient.Items.Add(new ListItem("",""));
            this.ddlClient.DataSource = cBll.GetModelList("");
            this.ddlClient.DataTextField = "clientName";
            this.ddlClient.DataValueField = "id";
            this.ddlClient.DataBind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = string.Empty;

            if (this.txtregCode.Text.Trim().Length == 0)
            {
                strErr += "商标注册号不能为空！\\n";
            }

            if (this.txtapplyDate.Text.Trim().Length > 0 && !PageValidate.IsDateTime(txtapplyDate.Text))
            {
                strErr += "申请日期格式错误！\\n";
            }
            if (this.txtspecialStartDate.Text.Trim().Length > 0 && !PageValidate.IsDateTime(txtspecialStartDate.Text))
            {
                strErr += "专用权期限开始日期格式错误！\\n";
            }
            if (this.txtspecialEndDate.Text.Trim().Length > 0 && !PageValidate.IsDateTime(txtspecialEndDate.Text))
            {
                strErr += "专用权期限结束日期格式错误！\\n";
            }
            if (string.IsNullOrEmpty(this.ddlBrandType.SelectedValue))
            {
                strErr += "商标类型不能为空！\\n";
            }
            if (this.txtproductList.Text.Trim().Length == 0)
            {
                strErr += "商品服务列表不能为空！\\n";
            }
            if (this.txtagentFee.Text.Trim().Length > 0 && !PageValidate.IsDecimal(txtagentFee.Text))
            {
                strErr += "代理费用格式错误！\\n";
            }
            if (this.txtnetApplyDate.Text.Trim().Length > 0 && !PageValidate.IsDateTime(txtnetApplyDate.Text))
            {
                strErr += "网上申请日期格式错误！\\n";
            }
            if (this.txtdocSubmitDate.Text.Trim().Length > 0 && !PageValidate.IsDateTime(txtdocSubmitDate.Text))
            {
                strErr += "资料交局日期格式错误！\\n";
            }
            if (this.txtassignDate.Text.Trim().Length > 0 && !PageValidate.IsDateTime(txtassignDate.Text))
            {
                strErr += "商标委托受理通知书下发日期格式错误！\\n";
            }
            if (string.IsNullOrEmpty(this.ddlStatus.SelectedValue))
            {
                strErr += "商标状态不能为空！\\n";
            }
            if (this.txtserviceExpire.Text.Trim().Length > 0 && !PageValidate.IsDateTime(txtserviceExpire.Text))
            {
                strErr += "服务到期日格式错误！\\n";
            }

            if (!string.IsNullOrEmpty(strErr))
            {
                MessageBox.Show(this, strErr);
                return;
            }


            UploadImage ui = new UploadImage();
            string uploadMsg =string.Empty;
            string iconPath =string.Empty;
            if (this.FileUpload1.HasFile)//上传了图标
            {
                ui.ImgThumbnail = true;
                ui.ImgLogo = false;
                iconPath = ui.UpLoadFiles(this.FileUpload1, out uploadMsg);
                if (string.IsNullOrEmpty(iconPath))
                {
                    MessageBox.Show(this, uploadMsg);
                    return; 
                }
            }            

            string icon = iconPath;
            string regCode = this.txtregCode.Text;
            //string typeCode = this.txttypeCode.Text;
            //DateTime applyDate = DateTime.Parse(this.txtapplyDate.Text);
            string applyManName = this.txtapplyManName.Text;
            string applyManAddress = this.txtapplyManAddress.Text;
            string applyManEnName = this.txtapplyManEnName.Text;
            string applyManEnAddress = this.txtapplyManEnAddress.Text;
            //DateTime specialStartDate = DateTime.Parse(this.txtspecialStartDate.Text);
            //DateTime specialEndDate = DateTime.Parse(this.txtspecialEndDate.Text);
            //string brandType = this.txtbrandType.Text;
            string clientService = this.txtclientService.Text;
            string productList = this.txtproductList.Text;
            //decimal agentFee = decimal.Parse(this.txtagentFee.Text);
            //DateTime netApplyDate = DateTime.Parse(this.txtnetApplyDate.Text);
            //DateTime docSubmitDate = DateTime.Parse(this.txtdocSubmitDate.Text);
            //DateTime assignDate = DateTime.Parse(this.txtassignDate.Text);
            //string status = this.txtstatus.Text;
            string memo = this.txtmemo.Text;

            XDSoft.Model.TB_Brand model = new XDSoft.Model.TB_Brand();
            model.icon = icon;
            model.regCode = regCode;
            model.typeCode = this.ddlTypeCode.SelectedValue;
            if (PageValidate.IsDateTime(this.txtapplyDate.Text))
                model.applyDate = Convert.ToDateTime(this.txtapplyDate.Text);
            else
                model.applyDate = null;
            model.applyManName = applyManName;
            model.applyManAddress = applyManAddress;
            model.applyManEnName = applyManEnName;
            model.applyManEnAddress = applyManEnAddress;
            if (PageValidate.IsDateTime(this.txtspecialStartDate.Text))
                model.specialStartDate = Convert.ToDateTime(this.txtspecialStartDate.Text);
            else
                model.specialStartDate = null;
            if (PageValidate.IsDateTime(this.txtspecialEndDate.Text))
                model.specialEndDate = Convert.ToDateTime(this.txtspecialEndDate.Text);
            else
                model.specialEndDate = null;
            model.brandType = this.ddlBrandType.SelectedValue;            
            model.clientService = clientService;
            model.productList = productList;
            if (this.txtagentFee.Text.Length > 0)
                model.agentFee = decimal.Parse(this.txtagentFee.Text);
            else
                model.agentFee = null;
            if (PageValidate.IsDateTime(this.txtnetApplyDate.Text))
                model.netApplyDate = Convert.ToDateTime(this.txtnetApplyDate.Text);
            else
                model.netApplyDate = null;
            if (PageValidate.IsDateTime(this.txtdocSubmitDate.Text))
                model.docSubmitDate = Convert.ToDateTime(this.txtdocSubmitDate.Text);
            else
                model.docSubmitDate = null;
            if (PageValidate.IsDateTime(this.txtassignDate.Text))
                model.assignDate = Convert.ToDateTime(this.txtassignDate.Text);
            else
                model.assignDate = null;
            model.status = this.ddlStatus.SelectedValue;
            if (!string.IsNullOrEmpty(this.ddlClient.SelectedValue))
                model.client = Convert.ToInt32(this.ddlClient.SelectedValue);
            model.memo = memo;
            if (PageValidate.IsDateTime(this.txtserviceExpire.Text))
                model.serviceExpire = Convert.ToDateTime(this.txtserviceExpire.Text);
            else
                model.serviceExpire = null;

            XDSoft.BLL.TB_Brand bll = new XDSoft.BLL.TB_Brand();
            if (bll.Add(model) > 0)
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "新增成功！", "add.aspx");
            else
                Maticsoft.Common.MessageBox.Show(this, "新增失败！");
        }
    }
}
